package com.example.shoppingmallspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.shoppingmallspringboot.entity.UserAddress;
import com.example.shoppingmallspringboot.mapper.UserAddressMapper;
import com.example.shoppingmallspringboot.service.UserAddressService;
import com.example.shoppingmallspringboot.util.FwResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public FwResult getUserAddress(Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserAddress> userAddressList = userAddressMapper.selectList(queryWrapper);
        if (userAddressList.size() > 0) {
            return FwResult.ok(userAddressList);
        } else {
            return FwResult.ok("您还没有添加地址");
        }
    }

    @Override
    public FwResult updateUserAddress(UserAddress userAddress) {
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<>();
        userAddressQueryWrapper.eq("id", userAddress.getId());
        List<UserAddress> userAddressList = userAddressMapper.selectList(userAddressQueryWrapper);
        
        if (userAddressList.size() > 0) {
            UpdateWrapper<UserAddress> updateWrapper = new UpdateWrapper<>();
            if (userAddress.getIsDefault() == 0) {
                updateWrapper.eq("user_id", userAddress.getUserId())
                        .set("is_default", 1);
                userAddressMapper.update(null, updateWrapper);
            }
            userAddressMapper.updateById(userAddress);
        } else {
            userAddressMapper.insert(userAddress);
            if (userAddress.getIsDefault() == 0) {
                UpdateWrapper<UserAddress> wrapper = new UpdateWrapper<>();
                wrapper.eq("user_id", userAddress.getUserId());
                wrapper.setSql("is_default = 1");
                userAddressMapper.update(null, wrapper);
                wrapper.setSql("is_default = if(id = last_insert_id(), 0, 1)");
                userAddressMapper.update(null, wrapper);
            }
            return FwResult.ok("添加成功");
        }
        return FwResult.ok();
    }

    @Override
    public FwResult deleteAddress(Integer id) {
        userAddressMapper.deleteById(id);
        return FwResult.ok("删除成功");
    }

    @Override
    public FwResult modifyAddressIsDefault(UserAddress userAddress) {
        UpdateWrapper<UserAddress> addressUpdateWrapper = new UpdateWrapper<>();
        addressUpdateWrapper.lambda().eq(UserAddress::getUserId, userAddress.getUserId());
        UserAddress notDefault = new UserAddress();
        notDefault.setIsDefault(1);
        userAddressMapper.update(notDefault, addressUpdateWrapper);
        
        addressUpdateWrapper.lambda().eq(UserAddress::getId, userAddress.getId());
        UserAddress defaultAddress = new UserAddress();
        defaultAddress.setIsDefault(userAddress.getIsDefault());
        userAddressMapper.update(defaultAddress, addressUpdateWrapper);
        return FwResult.ok();
    }
} 